package com.softeem.mpdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.mpdemo.dto.Result;
import com.softeem.mpdemo.entity.Listdetails;
import com.softeem.mpdemo.service.ListdetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Tag(name = "歌单详情管理",description = "歌曲和歌单详情管理工功能，添加歌单，删除歌单中歌曲，歌单详情查看等")
@CrossOrigin
@RestController
public class ListdetailsController {

    @Autowired
    private ListdetailsService listdetailsService;

    @Operation(summary = "添加歌曲到歌单",description = "根据提供的歌曲id和歌单id执行歌单详情添加操作")
    @Parameters({
        @Parameter(name = "listId",description = "歌单id",required = true),
        @Parameter(name = "mid",description = "歌曲id",required = true)
    })
    @PostMapping("/listdetails/add")
    public Result addDetails(Integer listId,Integer mid){
        //select * from listdetails where listid=? and mid=?
        QueryWrapper<Listdetails> query = Wrappers.query(Listdetails.class);
        query.eq("listid",listId).eq("mid",mid);
        //执行条件查询
        Listdetails one = listdetailsService.getOne(query);
        if(Objects.nonNull(one)){
            return Result.fail().setMsg("该歌曲已在歌单中存在");
        }
        Listdetails details = new Listdetails();
        details.setListid(listId);
        details.setMid(mid);
        //执行添加
        if(listdetailsService.save(details)){
            return Result.ok().setMsg("成功加入歌单");
        }
        return Result.fail().setMsg("添加失败");
    }
}
